<?php
namespace app\service;
use think\facade\Db;
use app\model\TransferModel;
use Exception;

class TransferService{  
    public function list($params){
        $page = $params['page'];
        $pageCount = $params['page_count'];
        $data = TransferModel::alias('a')
        ->field('a.*,a.from_bank as from_bank_id,a.to_bank as to_bank_id,b.bank_name as from_bank,c.bank_name as to_bank')
        ->join('bank b','a.from_bank=b.id','left')
        ->join('bank c','a.to_bank=c.id','left')
        ->page($page,$pageCount)->order('transfer_date','desc')->select(); 
        $count = TransferModel::count(); 
        return ["list"=>$data,"count"=>$count];
    } 

   
    public function save($params){              
        if(empty($params['id'])){           
            $from_bank=Db::name("Bank")->where('id',$params['from_bank'])->value('bank_name');
            $to_bank=Db::name("Bank")->where('id',$params['to_bank'])->value('bank_name');
            $params['title']=$from_bank."转到".$to_bank;
            $data = TransferModel::create($params); 
        }else{
            $from_bank=Db::name("Bank")->where('id',$params['from_bank'])->value('bank_name');
            $to_bank=Db::name("Bank")->where('id',$params['to_bank'])->value('bank_name');
            $params['title']=$from_bank."转到".$to_bank;
            $data = TransferModel::update($params, ['id' => $params['id']]); 
        }        
        return $data;
    }

    public function delete($params){   
        $data = TransferModel::destroy($params['id']);         
        return $data;
    }
}